package cn.xmy.demo01.servlet;

import cn.xmy.demo01.pojo.Address;
import cn.xmy.demo01.service.AddressService;
import cn.xmy.demo01.service.impl.AddressServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet("/addAddressServlet")
public class AddAddressServlet extends HttpServlet {

    // 处理GET请求
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        AddressService addressService = new AddressServiceImpl();
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String province = request.getParameter("province");
        String city = request.getParameter("city");
        String county = request.getParameter("county");
        String street = request.getParameter("street");
        String isDefault= request.getParameter("isDefault");
        Address address = new Address(null, name, phone, province, city, county, street,isDefault);
        int rows=0;
        try {
            rows=addressService.addAddress(address);
            PrintWriter out = response.getWriter();
            out.print(rows);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
